package cxl.attendance;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.filechooser.FileSystemView;

public class Util {

    private static final int DAY = Calendar.DAY_OF_YEAR;

    String fileName;
    DateFormat calDayFormat = new SimpleDateFormat("MM-dd");
    DateFormat calHourFormat = new SimpleDateFormat("HH:mm:ss");

    public Util(String fileName) {
        this.fileName = fileName;
    }

    public void execute(String name) {
        String line = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(getDesktopDir() + File.separator + fileName));

            ArrayList<Calendar> times = new ArrayList<>();
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
            while ((line = br.readLine()) != null) {
                String[] elements = line.split("\t");
                String regex = ".*" + name + ".*";
                if (elements.length < 2) {
                    break;
                }
                if (elements[1].matches(regex)) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(dateFormat.parse(elements[3] + " " + elements[4]));
                    if (times.size() == 0) {
                        times.add(cal);
                    } else {
                        Calendar firstCal = times.get(0);
                        if (cal.get(DAY) - firstCal.get(DAY) == 0) {
                            times.add(cal);
                        } else {
                            calculate(firstCal, times.get(times.size() - 1), cal);
                            times = new ArrayList<>();
                            times.add(cal);
                        }
                    }
                }

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    private void calculate(Calendar firstCal, Calendar lastCal, Calendar curCal) throws IOException {
        if (firstCal.get(Calendar.HOUR_OF_DAY) > 10) {
            System.out.println(getDayTime(firstCal) + "号 " + getHourTime(firstCal) + "上班考勤出现异常 ");
            writer(getDayTime(firstCal) + "号" + getHourTime(firstCal) + "上班考勤出现异常\r\n");
        }
        if (lastCal.get(Calendar.HOUR_OF_DAY) < 18) {
            System.out.println(getDayTime(lastCal) + "号 " + getHourTime(lastCal) + "下班考勤出现异常");
            writer(getDayTime(lastCal) + "号 " + getHourTime(lastCal) + "下班考勤出现异常\r\n");
        }
        if (lastCal.get(Calendar.HOUR_OF_DAY) > 19) {
            System.out.println(getDayTime(lastCal) + "号 " + getHourTime(lastCal) + "下班 出现加班情况");
            writer(getDayTime(lastCal) + "号 " + getHourTime(lastCal) + "下班 出现加班情况\r\n");
        }
        if (curCal.get(DAY) - firstCal.get(DAY) > 1) {
            for (int i = 1; i < curCal.get(DAY) - firstCal.get(DAY); i++) {
                firstCal.add(DAY, 1);

                int dayOfWeek = firstCal.get(Calendar.DAY_OF_WEEK);
                // 1和7 代表星期日和星期六
                if (dayOfWeek != 1 && dayOfWeek != 7) {
                    System.out.println(getDayTime(firstCal) + "号整天没上班");
                    writer(getDayTime(firstCal) + "号整天没上班\r\n");
                }
            }
        }
    }

    private String getDayTime(Calendar cal) {
        return calDayFormat.format(cal.getTime());
    }

    private String getHourTime(Calendar cal) {
        return calHourFormat.format(cal.getTime());
    }

    private String getDesktopDir() {
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        return desktopDir.getAbsolutePath();
    }

    private void writer(String content) throws IOException {
        FileWriter writer = new FileWriter(getDesktopDir() + File.separator + fileName, true);
        writer.write(content);
        writer.write("\n");
        writer.flush();
        writer.close();
    }
}
